var dataJson = [];
$(function(){
	$("#adduserType,#edituserType").combobox({
		data:[{"id":"2","text":"普通客户"}],
		valueField:'id',  
		textField:'text',
		editable:false
	})
	$("#addsex,#editsex").combobox({
		data:[{"id":"2","text":"女"},{"id":"1","text":"男"}],
		valueField:'id',  
		textField:'text',
		editable:false
	})
	$("#sex").combobox({
		data:[{"id":"2","text":"女"},{"id":"1","text":"男"}],
		valueField:'id',  
		textField:'text',
	})
	loadData();
	$("#custmerDatagrid").datagrid({
		data:dataJson,
		fit:true,
	    toolbar:'#custmerTolbar',
	    fitColumns:true,
	    rownumbers:true,
	    singleSelect:true,
	    columns:[[  
	        {field:'userId',hidden:true},
	        {field:'password',hidden:true},
	        {field:'userName',title:'姓名',width:100,align:"center"}, 
	        {field:'sex',title:'性别',width:80,align:"center",
	        	formatter:function(value,row,index){
	        		if('2' == value){
	        			return '女';
	        		}
	        		if('1' == value){
	        			return '男';
	        		}
	        	}	
	        },  
	        {field:'age',title:'年龄',width:100,align:"center"}, 
	        {field:'phone',title:'电话',width:100,align:"center"}, 
	        {field:'idCard',title:'身份证号',width:100,align:"center"}, 
	        {field:'userType',title:'用户类型',width:80,align:"center",
	        	formatter:function(value,row,index){
	        		if('2' == value){
	        			return '普通客户';
	        		}
	        		if('1' == value){
	        			return '管理员';
	        		}
	        	}	},
	        {field:'operation',title:'操作',width:100,align:"center",
	        	formatter:function(value,row,index){
	        		return " <a  href='#' onclick=editRow("+index+");  >编辑</a> "+
	                  " <a  href='#' onclick=deleteRow('"+row.userId+"'); >删除</a> ";
	        	}
	        	
	        },
	    ]]  
	
	});
	
	$("#addDialog").dialog({
		title: '新增用户信息',  
	    width: 310,  
	    height:330,  
	    closed: true,  
	    cache: false,    
	    modal: true ,
	    buttons:[{
			text:'保存',
			handler:function(){
				$.ajax({
					url:$WEB_ROOT_PATH+"/Customer/addUser.json",
					type:"post",
					data:JSON.stringify({
						'password':$("#addpassword").val(),
						'userName':$("#adduserName").val(),
						'sex':$("#addsex").combobox("getValue"),
						'age':$("#addage").val(),
						'phone':$("#addphone").val(),
						'idCard':$("#addidCard").val(),
						'userType':$("#adduserType").combobox("getValue")
					}),
					contentType: "application/json",
					dataType:"json",
					success:function(result){
						if(result.data.success == "0"){
							$.messager.alert("信息", "新增成功！",'info'); 
						}else{
							$.messager.alert("信息", "新增失败！",'info'); 
						}
						loadData();
						$("#custmerDatagrid").datagrid("loadData",dataJson);
						$("#addDialog").dialog("close");
					}
				})
			}
		},{
			text:'关闭',
			handler:function(){
				$("#addDialog").dialog("close");
			}
		}]
	});
	
	$("#editDialog").dialog({
		title: '编辑用户信息',  
	    width: 310,  
	    height:330,  
	    closed: true,  
	    modal: true ,
	    buttons:[{
			text:'保存',
			handler:function(){
				$.ajax({
					url:$WEB_ROOT_PATH+"/Customer/editUser.json",
					type:"post",
					data:JSON.stringify({
						'userId':$("#edituserId").val(),
						'password':$("#editpassword").val(),
						'userName':$("#edituserName").val(),
						'sex':$("#editsex").combobox("getValue"),
						'age':$("#editage").val(),
						'phone':$("#editphone").val(),
						'idCard':$("#editidCard").val(),
						'userType':$("#edituserType").combobox("getValue")
					}),
					contentType: "application/json",
					dataType:"json",
					success:function(result){
						if(result.data.success == "0"){
							$.messager.alert("信息", "修改成功！",'info'); 
						}else{
							$.messager.alert("信息", "修改失败！",'info'); 
						}
						loadData();
						$("#custmerDatagrid").datagrid("loadData",dataJson);
						$("#editDialog").dialog("close");
					}
				})
			}
		},{
			text:'关闭',
			handler:function(){
				$("#editDialog").dialog("close");
			}
		}]
	})
	
})
function loadData(){
	$.ajax({
		url:$WEB_ROOT_PATH+"/Customer/getUserList.json",
		type:"post",
		async: false, 
		data: JSON.stringify({"userName":'',"sex":''}),
		contentType: "application/json",
		dataType:"json",
		success:function(result){
			if(result.data.userInfoList== undefined &&result.data.userInfoList == null){
				dataJson = [];
			}else{
				dataJson = result.data.userInfoList;
			}
		}
	})
}
function doAdd(){
	$("#addDialog").dialog("center");
	$("#addDialog").dialog("open");
	$("#addDialogForm").form("clear");
	$("#adduserType").combobox("setValue","2");
}

function editRow(index){
	$("#editDialog").dialog("center");
	$("#editDialog").dialog("open");

	$('#custmerDatagrid').datagrid("selectRow",index);
	var row = $('#custmerDatagrid').datagrid("getSelected");
	if(!row){
		$('#custmerDatagrid').datagrid("selectRow",index);
		row = $('#custmerDatagrid').datagrid("getSelected");
	}
	$("#edituserId").val(row.userId);
	$("#edituserName").val(row.userName);
	$("#editsex").combobox("setValue",row.sex);
	$("#editage").val(row.age);
	$("#editphone").val(row.phone);
	$("#editidCard").val(row.idCard);
	$("#editpassword").val(row.password);
	$("#edituserType").combobox("setValue",row.userType);
}

function deleteRow(id){
	 $.messager.confirm("操作提示", "您确定要删除吗？", function (data) {  
         if (data) {  
        	 $.ajax({
     			url:$WEB_ROOT_PATH+"/Customer/deleteUser.json",
     			type:"post",
     			data:JSON.stringify({'userId':id}),
     			contentType: "application/json",
     			dataType:"json",
     			success:function(result){
     				if(result.data.success == "0"){
     					$.messager.alert("信息", "删除成功！",'info'); 
     				}else{
     					$.messager.alert("信息", "删除失败！",'info'); 
     				}
     				loadData();
     				$("#custmerDatagrid").datagrid("loadData",dataJson);
     			}
     		})  
         } else {  
            return;  
         }  
     }); 
}

function doSearch(){
	$.ajax({
		url:$WEB_ROOT_PATH+"/Customer/getUserList.json",
		type:"post",
		data: JSON.stringify({"userName":$("#userName").val(),"sex":$("#sex").combobox("getValue")}),
		contentType: "application/json",
		dataType:"json",
		success:function(result){
			if(result.data.userInfoList == undefined && result.data.userInfoList==null){
				dataJson = [];
			}else{
				dataJson = result.data.userInfoList;
			}
			$("#custmerDatagrid").datagrid("loadData",dataJson);
		}
	})
}